package com.yan.deezer.ui.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yan.deezer.R;
import com.yan.deezer.activity.DeezerDemoActivity;
import com.yan.deezer.data.manager.DataManager;
import com.yan.deezer.data.object.Artist;

public class ArtistListAdapter extends ArrayAdapter<Artist> implements
		OnItemClickListener {
	private static final String TAG = "YAN_DEEZER_ARTIST_LIST_ADAPTER";

	protected LayoutInflater mInflater;
	protected List<Artist> mList;
	protected int mnViewResId;
	protected Artist currentArtist;
	protected DataManager mDataManager;
	protected Context mContext;
	protected articlesViewHolder holder;

	public ArtistListAdapter(Context context, List<Artist> list, int resId) {
		super(context, resId, list);
		mInflater = LayoutInflater.from(context);
		mList = list;
		mnViewResId = resId;
		mDataManager = DataManager.getInstance();
		mContext = context;
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mList != null) {
			size = mList.size();
		}
		return size;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	static class articlesViewHolder {

		/*
		 * 
		 * 
		 * { "id": "27", "name": "Daft Punk", "link":
		 * "http://www.deezer.com/music/daft-punk", "picture":
		 * "http://api.deezer.com/2.0/artist/27/image", "nb_album": "48",
		 * "nb_fan": "271184", "radio": true, "type": "artist" }
		 */

		ImageView artistPicture;
		TextView artistName;

	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		holder = null;

		if (convertView == null) {
			convertView = mInflater.inflate(mnViewResId, null);
			holder = new articlesViewHolder();
			holder.artistPicture = (ImageView) convertView
					.findViewById(R.id.artist_image);
			holder.artistName = (TextView) convertView
					.findViewById(R.id.artist_name);

			convertView.setTag(holder);
		} else {
			holder = (articlesViewHolder) convertView.getTag();
		}

		currentArtist = getItem(position);

		int id = Integer.parseInt(currentArtist.getId());

		holder.artistPicture.setTag(id);

		holder.artistPicture.setImageResource(R.drawable.artist_default);

		mDataManager.setBitmapToImageView(currentArtist.getPicture(),
				holder.artistPicture, id, DataManager.getInstance()
						.getImageCacheForArtists());

		holder.artistName.setText(currentArtist.getName());
		holder.artistName.setSelected(true);
		holder.artistName.setCompoundDrawablePadding(3);

		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Artist artist = getItem(position);
		if (artist != null) {

			DeezerDemoActivity.getDeezerFragmentInterface()
					.addArtistAlbumListFragmentToStack(artist.getId());

		}

	}

}
